package com.qumitech.visualnovel.controller.rest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.util.CommonUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.model.dao.console.GiftStatisticDO;
import com.qumitech.visualnovel.model.dao.console.ProfitStatisticDO;
import com.qumitech.visualnovel.model.dao.console.OutputCommissionStatistics;
import com.qumitech.visualnovel.model.dao.console.StockStatistics;
import com.qumitech.visualnovel.model.dao.console.OutputConsumptionDetail;
import com.qumitech.visualnovel.service.EconomicStatisticsService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.qumitech.visualnovel.console.excel.ExportExcel;

import java.util.List;

@RestController
@RequestMapping("/api/economicStatistics")
public class EconomicStatisticsController {

    @Autowired
    EconomicStatisticsService economicStatisticsService;

    @RequestMapping(value = "/outputCommissionLine", method = RequestMethod.GET)
    public JSONObject outputCommissionLine(@RequestParam(value="channel", required=false) String channel,
                                   @RequestParam(value="version", required=false) String version,
                                   @RequestParam(value="propId", required=false) String propId,
                                   @RequestParam(value="startTime", required=false) String startTime,
                                   @RequestParam(value="endTime", required=false) String endTime) {
        JSONObject res = economicStatisticsService.getOutputCommissionStatisticsLine(channel, version, propId,startTime, endTime);
        return new BaseResponse<>(0,"success", res);
    }

    @RequestMapping(value = "/outputCommissionList", method = RequestMethod.GET)
    public JSONObject outputCommissionList(@RequestParam(value="channel", required=false) String channel,
                                           @RequestParam(value="version", required=false) String version,
                                           @RequestParam(value="propId", required=false) String propId,
                                           @RequestParam(value="startTime", required=false) String startTime,
                                           @RequestParam(value="endTime", required=false) String endTime,
                                           @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                           @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                                           @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                                           @RequestParam(value="orderBy", required=false, defaultValue="target_date") String orderBy) {

        JSONObject data = new JSONObject();
        JSONArray dataItems = new JSONArray();
        OutputCommissionStatistics outputCommissionStatistics = economicStatisticsService.getOutputCommissionStatisticsSum(channel, version, propId,startTime, endTime);
        data.put("count",economicStatisticsService.getOutputCommissionStatisticsCount(channel, version, propId, startTime, endTime));
        economicStatisticsService.getOutputCommissionStatisticsList(channel, version, propId, startTime, endTime, offset, limit, orderType, orderBy).forEach(
                x->{
                    dataItems.add(JSONObject.parseObject(JSON.toJSONString(x)));
                });
        data.put("items", dataItems);
        data.put("outputSum", outputCommissionStatistics.getOutput());
        data.put("commissionSum", outputCommissionStatistics.getCommission());
        return new BaseResponse(data);
    }

    @RequestMapping(value = "/outputLine", method = RequestMethod.GET)
    public JSONObject outputLine(@RequestParam(value="channel", required=false) String channel,
                                           @RequestParam(value="version", required=false) String version,
                                           @RequestParam(value="propId", required=false) String propId,
                                           @RequestParam(value="approach", required=false) Integer changeId,
                                           @RequestParam(value="startTime", required=false) String startTime,
                                           @RequestParam(value="endTime", required=false) String endTime) {
        JSONObject res = economicStatisticsService.getOutputStatisticsLine(channel, version, propId,changeId,
                startTime, endTime);
        return new BaseResponse<>(0,"success", res);
    }

    @RequestMapping(value = "/outputList", method = RequestMethod.GET)
    public JSONObject outputList(@RequestParam(value="channel", required=false) String channel,
                                           @RequestParam(value="version", required=false) String version,
                                           @RequestParam(value="propId", required=false) String propId,
                                           @RequestParam(value="approach", required=false) Integer changeId,
                                           @RequestParam(value="startTime", required=false) String startTime,
                                           @RequestParam(value="endTime", required=false) String endTime,
                                           @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                           @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                                           @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                                           @RequestParam(value="orderBy", required=false, defaultValue="target_date") String orderBy) {

        JSONObject data = new JSONObject();
        JSONArray dataItems = new JSONArray();
        data.put("count",economicStatisticsService.getOutputStatisticsCount(channel, version, propId,changeId, startTime, endTime));
        economicStatisticsService.getOutputStatisticsList(channel, version, propId,changeId, startTime, endTime, offset, limit, orderType, orderBy).forEach(
                x->{
                    dataItems.add(JSONObject.parseObject(JSON.toJSONString(x)));
                });
        data.put("items", dataItems);
        return new BaseResponse(data);
    }


    @RequestMapping(value = "/diamondOutputDetail", method = RequestMethod.GET)
    public JSONObject diamondOutputDetail(@RequestParam(value="channel", required=false) String channel,
                                   @RequestParam(value="startTime", required=false) String startTime,
                                   @RequestParam(value="endTime", required=false) String endTime,
                                   @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                   @RequestParam(value="limit", required=false, defaultValue="20") int limit) {

        JSONObject data = new JSONObject();
        JSONArray dataItems =  economicStatisticsService.diamondOutputDetail(channel, startTime, endTime);
        data.put("count", dataItems.size());
        data.put("items", CommonUtil.pageLimit(dataItems,offset,limit));
        return new BaseResponse(data);
    }

    @RequestMapping(value = "/diamondOutputDetail/percent", method = RequestMethod.GET)
    public JSONObject diamondOutputPercent(@RequestParam(value="channel", required=false) String channel,
                                           @RequestParam(value="startTime", required=false) String startTime,
                                           @RequestParam(value="endTime", required=false) String endTime) {
        return new BaseResponse(economicStatisticsService.diamondOutputPercent(channel, startTime, endTime));
    }

    @RequestMapping(value = "/coinOutputDetail", method = RequestMethod.GET)
    public JSONObject coinOutputDetail(@RequestParam(value="channel", required=false) String channel,
                                        @RequestParam(value="startTime", required=false) String startTime,
                                        @RequestParam(value="endTime", required=false) String endTime,
                                        @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                        @RequestParam(value="limit", required=false, defaultValue="20") int limit) {
        JSONObject data = new JSONObject();
        JSONArray dataItems =  economicStatisticsService.coinOutputDetail(channel, startTime, endTime);
        data.put("count", dataItems.size());
        data.put("items", CommonUtil.pageLimit(dataItems,offset,limit));
        return new BaseResponse(data);
    }

    @RequestMapping(value = "/hiCoinOutputDetail/percent", method = RequestMethod.GET)
    public JSONObject hiCoinOutputPercent(@RequestParam(value="channel", required=false) String channel,
                                   @RequestParam(value="startTime", required=false) String startTime,
                                   @RequestParam(value="endTime", required=false) String endTime) {
        return new BaseResponse(economicStatisticsService.hiCoinOutputPercent(channel, startTime, endTime));
    }

    @RequestMapping(value = "/hiCoinOutputDetail", method = RequestMethod.GET)
    public JSONObject hiCoinOutputDetail(@RequestParam(value="channel", required=false) String channel,
                                       @RequestParam(value="startTime", required=false) String startTime,
                                       @RequestParam(value="endTime", required=false) String endTime,
                                       @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                       @RequestParam(value="limit", required=false, defaultValue="20") int limit) {
        JSONObject data = new JSONObject();
        JSONArray dataItems =  economicStatisticsService.hiCoinOutputDetail(channel, startTime, endTime);
        data.put("count", dataItems.size());
        data.put("items", CommonUtil.pageLimit(dataItems,offset,limit));
        return new BaseResponse(data);
    }

    @RequestMapping(value = "/coinOutputDetail/percent", method = RequestMethod.GET)
    public JSONObject coinOutputPercent(@RequestParam(value="channel", required=false) String channel,
                                        @RequestParam(value="startTime", required=false) String startTime,
                                        @RequestParam(value="endTime", required=false) String endTime) {
        return new BaseResponse(economicStatisticsService.coinOutputPercent(channel, startTime, endTime));
    }

    @RequestMapping(value = "/diamondCommissionDetail", method = RequestMethod.GET)
    public JSONObject diamondCommissionDetail(@RequestParam(value="channel", required=false) String channel,
                                              @RequestParam(value="startTime", required=false) String startTime,
                                              @RequestParam(value="endTime", required=false) String endTime,
                                              @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                              @RequestParam(value="limit", required=false, defaultValue="20") int limit) {

        JSONObject data = new JSONObject();
        JSONArray dataItems =  economicStatisticsService.diamondCommissionDetail(channel, startTime, endTime);
        data.put("count", dataItems.size());
        data.put("items", CommonUtil.pageLimit(dataItems,offset,limit));
        return new BaseResponse(data);
    }

    @RequestMapping(value = "/diamondCommissionDetail/percent", method = RequestMethod.GET)
    public JSONObject diamondCommissionPercent(@RequestParam(value="channel", required=false) String channel,
                                               @RequestParam(value="startTime", required=false) String startTime,
                                               @RequestParam(value="endTime", required=false) String endTime) {
        return new BaseResponse(economicStatisticsService.diamondCommissionPercent(channel, startTime, endTime));
    }


    @RequestMapping(value = "/coinCommissionDetail", method = RequestMethod.GET)
    public JSONObject coinCommissionDetail(@RequestParam(value="channel", required=false) String channel,
                                           @RequestParam(value="startTime", required=false) String startTime,
                                           @RequestParam(value="endTime", required=false) String endTime,
                                           @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                           @RequestParam(value="limit", required=false, defaultValue="20") int limit) {
        JSONObject data = new JSONObject();
        JSONArray dataItems =  economicStatisticsService.coinCommissionDetail(channel, startTime, endTime);
        data.put("count", dataItems.size());
        data.put("items", CommonUtil.pageLimit(dataItems,offset,limit));
        return new BaseResponse(data);
    }

    @RequestMapping(value = "/coinCommissionDetail/percent", method = RequestMethod.GET)
    public JSONObject coinCommissionPercent(@RequestParam(value="channel", required=false) String channel,
                                           @RequestParam(value="startTime", required=false) String startTime,
                                           @RequestParam(value="endTime", required=false) String endTime) {
        return new BaseResponse(economicStatisticsService.coinCommissionPercent(channel, startTime, endTime));
    }


    @RequestMapping(value = "/hiCoinCommissionDetail", method = RequestMethod.GET)
    public JSONObject hiCoinCommissionDetail(@RequestParam(value="channel", required=false) String channel,
                                           @RequestParam(value="startTime", required=false) String startTime,
                                           @RequestParam(value="endTime", required=false) String endTime,
                                           @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                           @RequestParam(value="limit", required=false, defaultValue="20") int limit) {
        JSONObject data = new JSONObject();
        JSONArray dataItems =  economicStatisticsService.hiCoinCommissionDetail(channel, startTime, endTime);
        data.put("count", dataItems.size());
        data.put("items", CommonUtil.pageLimit(dataItems,offset,limit));
        return new BaseResponse(data);
    }

    @RequestMapping(value = "/hiCoinCommissionDetail/percent", method = RequestMethod.GET)
    public JSONObject hiCoinCommissionPercent(@RequestParam(value="channel", required=false) String channel,
                                            @RequestParam(value="startTime", required=false) String startTime,
                                            @RequestParam(value="endTime", required=false) String endTime) {
        return new BaseResponse(economicStatisticsService.hiCoinCommissionPercent(channel, startTime, endTime));
    }


    @RequestMapping(value = "/commissionLine", method = RequestMethod.GET)
    public JSONObject commissionLine(@RequestParam(value="channel", required=false) String channel,
                                 @RequestParam(value="version", required=false) String version,
                                 @RequestParam(value="propId", required=false) String propId,
                                 @RequestParam(value="approach", required=false) Integer changeId,
                                 @RequestParam(value="startTime", required=false) String startTime,
                                 @RequestParam(value="endTime", required=false) String endTime) {
        JSONObject res = economicStatisticsService.getCommissionStatisticsLine(channel, version, propId, changeId,
                startTime, endTime);
        return new BaseResponse<>(0,"success", res);
    }

    @RequestMapping(value = "/commissionList", method = RequestMethod.GET)
    public JSONObject commissionList(@RequestParam(value="channel", required=false) String channel,
                                           @RequestParam(value="version", required=false) String version,
                                           @RequestParam(value="propId", required=false) String propId,
                                           @RequestParam(value="approach", required=false) Integer changeId,
                                           @RequestParam(value="startTime", required=false) String startTime,
                                           @RequestParam(value="endTime", required=false) String endTime,
                                           @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                           @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                                           @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                                           @RequestParam(value="orderBy", required=false, defaultValue="target_date") String orderBy
    )
    {
        JSONObject data = new JSONObject();
        JSONArray dataItems = new JSONArray();
        data.put("count",economicStatisticsService.getCommissionStatisticsCount(channel, version, propId,changeId, startTime, endTime));
        economicStatisticsService.getCommissionStatisticsList(channel, version, propId,changeId, startTime, endTime, offset, limit, orderType, orderBy).forEach(
                x->{
                    dataItems.add(JSONObject.parseObject(JSON.toJSONString(x)));
                });
        data.put("items", dataItems);
        return new BaseResponse(data);
    }

    @RequestMapping(value = "/stockLine", method = RequestMethod.GET)
    public JSONObject stockLine(@RequestParam(value="channel", required=false) String channel,
                                @RequestParam(value="propId", required=false) String propId,
                                @RequestParam(value="startTime", required=false) String startTime,
                                @RequestParam(value="endTime", required=false) String endTime)
    {
        JSONObject res = economicStatisticsService.getStockStatisticsLine(channel, propId, startTime, endTime);
        return new BaseResponse<>(0,"success", res);
    }

    @RequestMapping(value = "/stockList", method = RequestMethod.GET)
    public JSONObject stockList(@RequestParam(value="channel", required=false) String channel,
                                @RequestParam(value="propId", required=false) String propId,
                                @RequestParam(value="startTime", required=false) String startTime,
                                @RequestParam(value="endTime", required=false) String endTime,
                                @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                                @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                                @RequestParam(value="orderBy", required=false, defaultValue="target_date") String orderBy
    )
    {
        JSONObject data = new JSONObject();
        JSONArray dataItems = new JSONArray();
        data.put("count",economicStatisticsService.getStockStatisticsCount(channel,propId, startTime, endTime));
        economicStatisticsService.getStockStatisticsList(channel,propId, startTime, endTime, offset, limit,
                orderType, orderBy).forEach(
                x->{
                    dataItems.add(x.toJsonObject());
                });
        data.put("items", dataItems);
        StockStatistics stockStatistics = economicStatisticsService.getStockStatisticsSum(channel, propId,
                startTime, endTime);
        data.put("changeDataSum", stockStatistics.getChangeData());
        return new BaseResponse(data);
    }

    @RequestMapping("/profit")
    public BaseRsp profitStatisticList(@RequestParam(value = "channel", required=false) String channel,
                              @RequestParam(value="startTime", required=false) String startTime,
                              @RequestParam(value="endTime", required=false) String endTime,
                              @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                              @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                              @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                              @RequestParam(value="orderBy", required=false, defaultValue="target_date") String orderBy)
    {
        List<ProfitStatisticDO> profitStatisticDOList = economicStatisticsService.profitStatisticList(channel,
                startTime, endTime, offset, limit, orderType, orderBy);
        Integer count = economicStatisticsService.countProfitStatistic(channel, startTime, endTime);
        ProfitStatisticDO statisticDO = economicStatisticsService.sumProfitStatistic(channel, startTime, endTime);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("profit", profitStatisticDOList, "count", count, "statistic", statisticDO));
    }

    @RequestMapping("/gift/list")
    public BaseRsp giftStatisticList(@RequestParam(value = "channel", required=false) String channel,
                                     @RequestParam(value="startTime", required=false) String startTime,
                                     @RequestParam(value="endTime", required=false) String endTime,
                                     @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                     @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                                     @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                                     @RequestParam(value="orderBy", required=false, defaultValue="target_date") String orderBy)
    {
        List<GiftStatisticDO> giftStatisticDOList = economicStatisticsService.giftStatisticList(channel,
                startTime, endTime, offset, limit, orderType, orderBy);
        Integer count = economicStatisticsService.countGiftStatistic(channel, startTime, endTime);
        GiftStatisticDO statisticDO = economicStatisticsService.sumGiftStatistic(channel, startTime, endTime);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("gift", giftStatisticDOList, "count", count, "statistic", statisticDO));
    }

    @RequestMapping("/gift/line")
    public JSONObject giftStatisticLine(@RequestParam(value = "channel", required=false) String channel,
                                     @RequestParam(value="startTime", required=false) String startTime,
                                     @RequestParam(value="endTime", required=false) String endTime){
        JSONObject res = economicStatisticsService.getGiftStatisticLine(channel, startTime, endTime);
        return new BaseResponse<>(0,"success", res);
    }

    @RequestMapping(value = "/outputConsumptionDetail", method = RequestMethod.GET)
    @ExportExcel(itemCls=OutputConsumptionDetail.class, listKey="items",
      itemKeys={"dtdate", "uid", "nickname", "channel", "payAll", "payHicoin", "payDiamond", "consumeHicoin", "consumeDiamond", "giftDiamond"},
      itemNames={"日期", "UID", "昵称", "注册渠道", "商城充值/元", "嗨币充值/元", "钻石充值/元", "嗨币消耗/币", "钻石消耗/钻", "商城礼物消耗/钻"})
    public BaseRsp outputConsumptionDetail(
        @RequestParam(value = "uid", required = false) String uid,
        @RequestParam(value = "channel", required = false) String channel,
        @RequestParam(value = "startTime", required = false) String startTime,
        @RequestParam(value = "endTime", required = false) String endTime,
        @RequestParam(value = "offset", required = false, defaultValue="0") String offset,
        @RequestParam(value = "limit", required = false, defaultValue="20") String limit,
        @RequestParam(value = "orderBy", required=false, defaultValue="dtdate") String orderBy,
        @RequestParam(value = "orderType", required=false, defaultValue="DESC") String orderType) {
      JSONObject res = economicStatisticsService.getOutputConsumptionDetail(uid, channel, startTime, endTime, orderBy, orderType, offset, limit);
      return new BaseRsp(ResultEnum.SUCCESS, res);
    }

    @RequestMapping(value = "/rechargeStatistic", method = RequestMethod.GET)
    public BaseRsp getRechargeStatisticList(
        @RequestParam(value = "channel", required = false) String mixChannel,
        @RequestParam(value = "startTime", required = false) String startTime,
        @RequestParam(value = "endTime", required = false) String endTime,
        @RequestParam(value = "offset", required = false, defaultValue="0") Integer offset,
        @RequestParam(value = "limit", required = false, defaultValue="20") Integer limit) {
      String[] channels = mixChannel!=null ? mixChannel.split(":") : null;
      String channel = (channels!=null && channels.length>0) ? channels[0] : null;
      String subChannel = (channels!=null && channels.length>1) ? channels[1] : null;
      JSONObject res = economicStatisticsService.getRechargeStatisticList(channel, subChannel, startTime, endTime, offset, limit);
      return new BaseRsp(ResultEnum.SUCCESS, res);
    }
}
